package ua.com.kangaroo.writer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import ua.com.kangaroo.basic.SchoolsData;

public class BaseWriter {
	private File baseFile;
	
	public BaseWriter(String baseFileName) {	
		baseFile = new File(baseFileName);
	}
	
	/**
	 * @return true if it can write information to file baseFile and true if not
	 */
	public boolean writeToFile(){
		BufferedWriter FileBuffer = null;
		try{
			FileBuffer = new BufferedWriter(new  FileWriter(baseFile));
			int idOfFirstSchool = 999;
			idOfFirstSchool = SchoolsData.getNextSchool(idOfFirstSchool);
			FileBuffer.write(SchoolsData.getSchoolById(idOfFirstSchool).toBase());
			int i = SchoolsData.getNextSchool(idOfFirstSchool);
			while (i != idOfFirstSchool) {
				FileBuffer.newLine();
				FileBuffer.write(SchoolsData.getSchoolById(i).toBase());
				i = SchoolsData.getNextSchool(i);
			}
			FileBuffer.newLine();
			return true;
		} catch (IOException e) {
			return false;
		} finally {
			if (FileBuffer != null)
				try {
					FileBuffer.flush();
					FileBuffer.close();
				} catch (IOException e) {
				}
		}
	}


}
